<?php

class TutorialsController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/triple_sidebar_courses';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','createanswer','updateanswer'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete','deleteanswer'),
				'roles'=>array('teacher'),
				'users'=>array('*'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$model0 = Tutorials::model()->findByPk($id)->tutorial_type_id;
		$model1 = TutorialType::model()->findByAttributes(array('id'=>$model0));
		$model2 = FileAssignment::model()->findAllByAttributes(array('tutorial_id'=>$id));
		$model3 = Tutorials::model()->findByPk($id);
		$model4 = FileAssignment::model()->findByAttributes(array('tutorial_id'=>$id));
		$model5 = new ForumType;
		$model6 = new Forum;
		$model7 = new ForumQuestionAssignment;
		$model8 = new ForumAnswerAssignment;
		$answers = new ForumAnswers;
		
		$question = $this->createQuestion($id);
		//$variable = Announcements::model()->findAll();
		//$vatiable = Announcements::model()->findByAttributes(array('id'=>2))->title;
		
		$this->render('view',array(
			'model'=>$this->loadModel($id),
			'model1'=>$model1,
			'model2'=>$model2,
			'model4'=>$model4,
			'model5'=>$model5,
			'model6'=>$model6,
			'model7'=>$model7,
			'model8'=>$model8,
			'question'=>$question,
			'answers'=>$answers,
			//'ann'=>$vatiable,
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Tutorials;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Tutorials']))
		{
			$model->attributes=$_POST['Tutorials'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$tutorials = Tutorials::model()->findAll();
		$first_id = 0;
		if($tutorials == null){
				$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
			}
		foreach($tutorials as $tut){
				$first_id = $tut->id;
				break;
			}
		$this->redirect(array('view', 'id'=>$first_id));
		$dataProvider=new CActiveDataProvider('Tutorials');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}


	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Tutorials the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Tutorials::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Tutorials $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='tutorials-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	public function createQuestion($tutorial_id){
			$question = new Forum;
			$assign = new ForumQuestionAssignment;
			$temp;
			if(isset($_POST['Forum'])){
					$question->attributes = $_POST['Forum'];
					$question->answer_count = 0;
					$question->DateCreated = date_create()->format('Y-m-d H:i:s');
					$temp = $question->DateCreated;
					$question->user_id = Yii::app()->user->getId();
					$question->save();
					$assign->tutorial_id = $tutorial_id;
					$assign->question_id = Forum::model()->findByAttributes(array('DateCreated'=>$temp))->id;
					$assign->save();
					$this->redirect(array('tutorials/view', 'id'=>$tutorial_id));
				}
				
			return $question;
		}
		
	public function actionCreateAnswer($qid, $tid){
			$answer = new ForumAnswers;
			if(isset($_POST['ForumAnswers'])){
				$answer->attributes = $_POST['ForumAnswers'];
				$answer->DateCreated = date_create()->format('Y-m-d H:i:s');
				$answer->user_id = Yii::app()->user->getId();
				$temp_date = $answer->DateCreated;
				if($answer->save()){
						$new_answer_assignment = new ForumAnswerAssignment;
						$new_answer_assignment->question_id = $qid;
						$new_answer_assignment->answer_id = ForumAnswers::model()->findByAttributes(array('DateCreated'=>$temp_date))->id;
						$new_answer_assignment->insert();
						$this->redirect(array('tutorials/view', 'id'=>$tid));
					}
				}
		}
		
	public function actionDelete($id, $tid){ 
			foreach(ForumAnswerAssignment::model()->findAllByAttributes(array('question_id'=>$id)) as $answers){
					ForumAnswers::model()->deleteByPk($answers->answer_id);
				}
			ForumAnswerAssignment::model()->deleteAllByAttributes(array('question_id'=>$id));
			ForumQuestionAssignment::model()->deleteAllByAttributes(array('question_id'=>$id));
			Forum::model()->deleteByPk($id);
			$this->redirect(array('tutorials/view', 'id'=>$tid));
		}
		
	public function actionDeleteAnswer($id, $tid){ 
			ForumAnswerAssignment::model()->deleteAllByAttributes(array('answer_id'=>$id));
			ForumAnswers::model()->deleteByPk($id);
			$this->redirect(array('tutorials/view', 'id'=>$tid));
		}
		
	public function actionUpdate($id, $tid){
			$model6 = Forum::model()->findByPk($id);
			$answers = ForumAnswers::model()->findByPk($id);
			$model0 = Tutorials::model()->findByPk($tid)->tutorial_type_id;
			$model1 = TutorialType::model()->findByAttributes(array('id'=>$model0));
			$model2 = FileAssignment::model()->findAllByAttributes(array('tutorial_id'=>$tid));
			$model3 = Tutorials::model()->findByPk($tid);
			$model4 = FileAssignment::model()->findByAttributes(array('tutorial_id'=>$tid));
			$model5 = new ForumType;
			$model7 = new ForumQuestionAssignment;
			$model8 = new ForumAnswerAssignment;
			//$answers = new ForumAnswers;
			$question = $this->saveChangedQuestions($id, $tid);
			
			$this->render('view', array(
			'id'=>$tid,
			'model'=>$this->loadModel($tid),
			'model1'=>$model1,
			'model2'=>$model2,
			'model4'=>$model4,
			'model5'=>$model5,
			'model6'=>$model6,
			'model7'=>$model7,
			'model8'=>$model8,
			'question'=>$question,
			'answers'=>$answers,
				));
		}
		
	public function actionUpdateAnswer($id, $tid){
			$answers = ForumAnswers::model()->findByPk($id);
			$model0 = Tutorials::model()->findByPk($tid)->tutorial_type_id;
			$model1 = TutorialType::model()->findByAttributes(array('id'=>$model0));
			$model2 = FileAssignment::model()->findAllByAttributes(array('tutorial_id'=>$tid));
			$model3 = Tutorials::model()->findByPk($tid);
			$model4 = FileAssignment::model()->findByAttributes(array('tutorial_id'=>$tid));
			$model5 = new ForumType;
			$model6 = new Forum;
			$model7 = new ForumQuestionAssignment;
			$model8 = new ForumAnswerAssignment;
			//$answers = new ForumAnswers;
			$question = $this->saveChangedAnswers($id, $tid);
			
			$this->render('view', array(
			'id'=>$tid,
			'model'=>$this->loadModel($tid),
			'model1'=>$model1,
			'model2'=>$model2,
			'model4'=>$model4,
			'model5'=>$model5,
			'model6'=>$answers,
			'model7'=>$model7,
			'model8'=>$model8,
			'question'=>$question,
			'answers'=>$answers,
				));
		}
		
	public function saveChangedAnswers($ans_id, $tid){
		$answer = ForumAnswers::model()->findByPk($ans_id);
		$new_answer = new ForumAnswers;
			if(isset($_POST['ForumAnswers'])){
				$new_answer->attributes = $_POST['ForumAnswers'];
				$answer->Content = $new_answer->Content;
				$answer->update();
				$this->redirect(array('tutorials/view', 'id'=>$tid));
				}
		}
		
	public function saveChangedQuestions($q_id, $tid){
		$question = Forum::model()->findByPk($q_id);
		$new_question = new ForumAnswers;
			if(isset($_POST['Forum'])){
				$new_question->attributes = $_POST['Forum'];
				$question->Content = $new_question->Content;
				$question->update();
				$this->redirect(array('tutorials/view', 'id'=>$tid));
				}
		}
}
